package com.tmt.annotation.parser;

import com.tmt.annotation.ResourceIdDescriptor;
import com.tmt.helper.ReflectHelper;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * 该类用于读取字段上的ResourceIdDescriptor注解的属性值。
 * Created by ThreeManTeam on 201711/14.
 */
public class ResourceIdDescriptorParser extends DescriptorParser<ResourceIdDescriptor> {

    public ResourceIdDescriptorParser(Class<ResourceIdDescriptor> annotationClazz) {
        super(annotationClazz);
    }

    /**
     * 获取clazz类上的fieldName字段的ResourceIdDescriptor注解的scope属性值。
     * @param clazz
     * @param fieldName
     * @return
     */
    public String[] getScope(Class<?> clazz, String fieldName){
        Field target = ReflectHelper.findFieldByName(clazz, fieldName);
        return _getScope(target);
    }


    /***************************************************************************************************/
    /**********************私有方法*********************************************************************/


    private String[] _getScope(Field target){
        if (target != null) {
            ResourceIdDescriptor[] array = target.getAnnotationsByType(ResourceIdDescriptor.class);
            if (array.length > 0) {
                String[] scope = array[0].scope();
                return scope.length > 0 ?  scope : new String[]{"DEFAULT"};
            }
        }
        return new String[]{"DEFAULT"};
    }
}
